package com.sdtv.mediunfusion.mvp.ui.meltcall;


import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sdtv.basemodule.annotations.CreatePresenter;
import com.sdtv.basemodule.annotations.PresenterVariable;
import com.sdtv.basemodule.base.BaseActivity;
import com.sdtv.mediunfusion.R;
import com.sdtv.mediunfusion.bean.MeltCallBean;
import com.sdtv.mediunfusion.mvp.contract.MainContract;
import com.sdtv.mediunfusion.mvp.presenter.MeltCallPresenter;
import com.sdtv.mediunfusion.mvp.ui.meltcall.adapter.CallListAdapter;
import com.sdtv.mediunfusion.utils.Constants;
import com.sdtv.mediunfusion.utils.RouterPath;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 通话记录页面
 * 显示通话记录
 */
@CreatePresenter(presenter = MeltCallPresenter.class)
@Route(path = RouterPath.TO_MELTCALL_LIST_ACTIVITY)
public class MeltCallListActivity extends BaseActivity implements MainContract.MeltCallView {

    @BindView(R.id.meltcalllistView)
    RecyclerView recyclerView;
    @BindView(R.id.xrefreshview)
    SmartRefreshLayout refreshLayout;

    private int currentPage = 0;

    @PresenterVariable
    MeltCallPresenter presenter;
    // 数据列表
    private List<MeltCallBean> dataList = new ArrayList<>();
    // 数据适配器
    private CallListAdapter adapter;

    /**
     * 设置布局文件
     *
     * @return
     */
    @Override
    public int getLayoutId() {
        return R.layout.activity_melt_call_list;
    }

    /**
     * 初始化View
     */
    @Override
    public void initView() {
        // 设置recyclerview属性
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        // 设置刷新事件
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            //刷新事件 --从第一页开始加载数据
            currentPage = 0;
            dataList = new ArrayList<>();
            initData();
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            // 加载更多事件 ---从首页开始加载数据
            currentPage += Constants.PAGE_SIZE;
            initData();
        });
        // 设置数据
        adapter = new CallListAdapter(R.layout.melt_call_item, dataList);
        recyclerView.setAdapter(adapter);
    }

    /**
     * 加载数据开始
     */
    @Override
    public void initData() {
        presenter.getCallList(currentPage);
    }

    /**
     * 设置页面数据
     *
     * @param responseList
     */
    @Override
    public void setCallDataList(int totalCount, List<MeltCallBean> responseList) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        dataList.addAll(responseList);
        adapter.setNewData(dataList);
        // 判断是否显示加载更多
        if (totalCount <= dataList.size()) {
            //全部数据已经加载完成 不显示加载更多
            refreshLayout.finishLoadMoreWithNoMoreData();
        }


    }

    /**
     * 未获取到数据
     */
    @Override
    public void showEmptyView() {
        // 如果列表已经有数据了 则此时的empty是结束加载更多
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();
        if (null != adapter && adapter.getData().isEmpty()) {
            // 显示暂无内容
            adapter.setEmptyView(R.layout.call_no_data_layout, recyclerView);
        } else {
            refreshLayout.finishLoadMoreWithNoMoreData();
        }
    }


    /**
     * 显示错误页面
     */
    @Override
    public void onError() {
        //如果是有数据了 则是刷新错误 --如果没有数据 则是加载错误
        if (null != adapter && adapter.getData().isEmpty()) {
            super.onError();
        } else {
            //刷新或者加载更多失败
            if (currentPage > 0) {
                // 加载更多错误
                refreshLayout.finishLoadMore(false);
            } else {
                // 刷新错误
                refreshLayout.finishRefresh(false);
            }


        }
    }
}
